import {HTTP} from "../../utils/http.js"
import {FriendModel} from "../../models/friend.js";
import {ActModel} from "../../models/ActModel.js";
import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog';
import { WorkModel } from "../../models/work.js";
import { UserModel } from "../../models/user.js";
var _UserModel = new UserModel();
const _WorkModel = new WorkModel();
var friend = new FriendModel();
var actBean =new ActModel();
var http = new HTTP();
const app = getApp();
var _self
Page({
  data: {
      userName:"",
      startDate:"",
      wechatProfileUrl:"",
      mobile:"",
      isshowmobile:0,
      percent:0,
      sumBPNumver:0,
      sumInviteNum:"",
      sumBePraised:0,
      sumBPLv:"",
      activePointLv:"",
      eduInfo:[],
      friendCount: "",
      address:"",
      degree:"",
      subject:"",
      positionTitle:"",
      geographicLocation:"",
      usrUserLables:[],
      workExpInfo:null,
      selfIntroduction:"",
      companyTitle:"",
    newMessage:0
    },
    onShow:function(options){
      _UserModel.getUsrMessage(function (data) {
        _self.setData({
          newMessage: data[0].list.length
        })
        if(data[0].list.length > 0) {
          wx.setTabBarBadge({
            index: 2,
            text: data[0].list.length+''
          })
        }else{
          wx.removeTabBarBadge({
            index: 2
          })
        }
        

     
      })  
    },
  takePhoto:function(){
    wx.scanCode({
      onlyFromCamera: false,
      success(res) {
        if (res.scanType =="QR_CODE"){
          let result = res.result;
          if(result.indexOf("UID")!=-1){
            //添加好友
            var userArr = result.split("=");
            if(userArr[1].length>0){
              friend.addFriend(userArr[1],function (data) {
                Dialog.alert({
                  message: "成功发送添加好友请求。"
                });
                return;
              })
            }else{

            }
             
          }else{
             //活动签到
            let act_id = result;
            if (act_id != "") {
              actBean.sign(act_id, function () {
                Dialog.alert({
                  message: "签到成功。"
                });
                return;
              });
            }
          }
        }
      },
      fail:function(error){
       
      },
      complete:function(){
      
      }
    })
  },
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true
    });
   
    _self = this;
    this.getUserBasicInfo();
    this.getCourse();
    this.getUserExtend();
    this.getEduInfo();
    _WorkModel.fetch(function (data) {
      if(data.length>0){
        _self.setData({
          workExpInfo: data[0]
        })
      }
   
    })
  },
  getCourse:function(){
    http.request({
      url: "/usr/getCourse",
      method: "POST",
      success: function (data) {
         let percent = data[0];
         console.log(percent)
         _self.setData({
           percent:percent
         })
      }
    });
  },
  getUserExtend:function(){
    http.request({
      url: "/usr/showInf",
      method: "POST",
      success: function (data) {
        var info = data[0];
          _self.setData({
            sumBPNumver: info.sumBPNumver,
            sumBePraised: info.sumBePraised,
            sumInviteNum: info.sumInviteNum,
            sumBPLv: info.sumBPLv,
            activePointLv: info.activePointLv,
            selfIntroduction: info.selfIntroduction,
            usrUserLables: info.usrUserLables
          })
      }
    });
  },
  getUserBasicInfo:function(){
    http.request({
      url: "/usr/personInf",
      method: "POST",
      success: function (data) {
        let userInfo = data[0];
        app.globalData.personalInfo = userInfo
        console.log("/usr/personInf", app.globalData.personalInfo);
       _self.setData({
         startDate: userInfo.startDate.substr(0,4)+"级",
         userName: userInfo.userName,
         wechatProfileUrl: userInfo.wechatProfileUrl,
         mobile: userInfo.mobile,
         isshowmobile: userInfo.user.isshowmobile,
         friendCount:userInfo.friendCount,
         address:userInfo.address,
         degree: userInfo.Eucational_Background,
         geographicLocation: userInfo.geographicLocation,
         subject: userInfo.specializedSubject,
         positionTitle:userInfo.title
       })

        if (userInfo.usrWorkExperience.length>0){
          let workInfo = userInfo.usrWorkExperience[0];
          _self.setData({
            companyTitle: workInfo.work
          })
        }
      }
    })
  },
  getEduInfo:function(){
    http.request({
      url: "/usr/educationalExperience",
      method: "POST",
      success: function (data) {
        if(data.length>0){
          _self.setData({
            eduInfo: data[0]
          })
        }
      }
    })
  },
  onShareAppMessage: function (options) {
      console.log(options);
    if (options.from === 'button') {
      
      let user = wx.getStorageSync("usercache");
      if (options.target.dataset.available =='invite'){
        console.log("invite 分支")
            return {
                desc: '邀请好友加入内大校友。',
              path: 'pages/wxlogin/wxlogin?inviteUserId=' + user.userId + '&inviteUserName=' + user.userName,
                imageUrl: "https://admin.51imu.com/file/resources/imulogo.jpg",
                success: function (res) {
                  wx.getShareInfo({
                    shareTicket: res.shareTickets[0],
                    success: function (res) {
                      console.log("share", res)
                    },
                    fail: function (res) { console.log(res) },
                    complete: function (res) { console.log(res) }
                  });
                },
                fail: function () {
                  
                }
              }
      }

      if (options.target.dataset.available =='shareMyCard'){
        console.log("shareMyCard 分支")
        return {
          desc: '' + user.userName,
          path: 'pages/board/board?shareUserId=' + user.userId,
          success: function (res) {
            wx.getShareInfo({
              shareTicket: res.shareTickets[0],
              success: function (res) {
                console.log("share", res)
              },
              fail: function (res) { console.log(res) },
              complete: function (res) { console.log(res) }
            });
          },
          fail: function () {

          }
        }
      }
    }

    if (options.from === 'menu'){
      return {
        desc: '邀请好友加入内大校友。',
        path: 'pages/wxlogin/wxlogin?',
        imageUrl: "https://admin.51imu.com/file/resources/imulogo.jpg",
        success: function (res) {

        },
        fail: function () {
          console.log("2")
        }
      }
    }

  },
  showMyQR:function(){
    wx.navigateTo({
      url: '/pages/profile/myqr/myqr'
    })
  },
  changeData:function(action){
    if(action=='updateIntroduction'){
      _self.getUserExtend();
    }
    if (action == 'userInfoChanged') {
      _self.getUserBasicInfo();
    }
    

  },
  preview:function(){
    let urls = new Array(_self.data.wechatProfileUrl)
    wx.previewImage({
      current: _self.data.wechatProfileUrl,
      urls: urls
    })
  }
})